package com.example.nuc_ms_beta.ui.viewconstruction.moderfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import com.bumptech.glide.Glide;
import com.example.nuc_ms_beta.R;
import com.example.nuc_ms_beta.app.MyApplication;
import com.example.nuc_ms_beta.bean.growservice.GrowService_Banner;
import com.example.nuc_ms_beta.ui.viewconstruction.mainfragment.NewsFragment;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.bgabanner.BGABanner;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;


/**
 * Created by 张博川 on 2016/9/28.  成长服务
 */
public class GrowServiceFragment extends Fragment {
    private View view;//主View


    private BGABanner growservice_newsbanner;
    private ListView growservice_listView;


    private List<String> newsbanner__image = new ArrayList<>();
    private List<String> newsbanner_title = new ArrayList<>();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView(inflater, container);
        return view;
    }

    private void initView(LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.growservicefragment, container, false);
        growservice_newsbanner = (BGABanner) view.findViewById(R.id.growservice_newsbanner);
        growservice_listView = (ListView) view.findViewById(R.id.growervice_listview);
        growservice_newsbanner.setAdapter(new BGABanner.Adapter<ImageView, String>() {
            @Override
            public void fillBannerItem(BGABanner banner, ImageView itemView, String model, int position) {
                Glide.with(NewsFragment.context)
                        .load(model)
                        .placeholder(R.mipmap.ic_launcher)
                        .error(R.mipmap.ic_launcher)
                        .centerCrop()
                        .dontAnimate()
                        .into(itemView);
            }
        });
        queryBannerData();

    }

    private void queryBannerData() {
        newsbanner__image.clear();
        newsbanner_title.clear();
        BmobQuery<GrowService_Banner> query_banner = new BmobQuery<>();
        query_banner.findObjects(new FindListener<GrowService_Banner>() {
            @Override
            public void done(List<GrowService_Banner> list, BmobException e) {
                if (e == null) {
                    for (GrowService_Banner banner : list) {
                        newsbanner__image.add(banner.getBanner_image().getFileUrl());
                        newsbanner_title.add(banner.getBanner_title());
                    }
                    growservice_newsbanner.setData(newsbanner__image, newsbanner_title);
                } else {
                    Log.d(MyApplication.TAG, e.getErrorCode() + "  " + e.getMessage());
                }
            }
        });
    }

    public static GrowServiceFragment getInstance() {
        GrowServiceFragment fragment = new GrowServiceFragment();
        return fragment;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        newsbanner__image.clear();
        newsbanner_title.clear();

    }
}
